package com.as3.timbot.iso
{
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.events.Event;
	
	import com.as3.timbot.iso.Isometric;
	import com.as3.timbot.iso.ActorMoveCommand;
	import com.as3.timbot.command.CommandEvent;
	import com.as3.timbot.command.CommandQueue;
	
	public class Actor
	{
		private var __graphic:MovieClip;
		private var __graphicShadow:MovieClip;
		private var __speed:Number;
		private var __actorMoveTargetX:Number;
		private var __actorMoveTargetY:Number;
		private var __shadowMoveTargetX:Number;
		private var __shadowMoveTargetY:Number;
		private var __xmov:Number;
		private var __ymov:Number;
		private var moveQueue:CommandQueue;
		
		private var iso:Isometric;
		
		public function Actor()
		{
			moveQueue = new CommandQueue();
		}
		public function setIso(value:Isometric):void
		{
			iso = value;//has coordinates of isometric world
		}
		public function setGraphic(value:MovieClip,shadowValue:MovieClip):void
		{
			__graphic = value;
			__graphicShadow = shadowValue;
		}
		public function getGraphic():Array
		{
			return new Array(__graphic,__graphicShadow);
		}
		public function walkPath():void
		{
			moveQueue.execute();
		}
		//pass in a move path, not just an endtarget...............t.w.
		public function plot(x:Number,y:Number,z:Number):void
		{
			//move the actor to a point - how it moves should be subclassed to 
			//the individual actor types. i.e. boat extends Actor, plane extends Actor, etc.
			var temp:Array = iso.mapToScreen(x, y, z);
			__actorMoveTargetX = temp[0];
			__actorMoveTargetY = temp[1]+10;
			temp = iso.mapToScreen(x, 0, z);
			__shadowMoveTargetX = temp[0];
			__shadowMoveTargetY = temp[1]+10;
						
			//@ param actor, movetargetX, movetargetY
			var myCommand:ActorMoveCommand = new ActorMoveCommand(this,__actorMoveTargetX,__actorMoveTargetY,__shadowMoveTargetX,__shadowMoveTargetY);
			myCommand.addEventListener(CommandEvent.COMPLETE,moveComplete);
			myCommand.addEventListener(CommandEvent.START,onStart);
			myCommand.addEventListener(CommandEvent.PROGRESS,onProgress);
			
			moveQueue.add(myCommand);
								
		}
		public function positionActor(x:Number,y:Number,sx:Number,sy:Number):void
		{
			__graphic.x = x;
			__graphic.y = y;
			__graphicShadow.x = sx;
			__graphicShadow.y = sy;
			////////////////////////////////////////
			var xm:Number = x;
			var zm:Number = y;
			var startx = __graphic.x;
			var startz = __graphic.y;
			var angleSpan:Number = 360/8;
			var angle:Number = Math.atan2(zm-startz, xm-startx);
			var realAngle:Number = angle*180/Math.PI;
			realAngle += angleSpan/2;
			if (realAngle<0) {
				realAngle += 360;
			}
			var frame:Number = Math.ceil(realAngle/angleSpan);
			__graphic.gotoAndStop("walk"+frame);
			__graphic.x = x;
			__graphic.y = y;
			__graphicShadow.x = sx;
			__graphicShadow.y = sy;
		//////////////////////////////////////////
		}
		///////////////////////////////////////////
		public function moveComplete(ev:CommandEvent):void
		{
			//trace("The command was completed");
		}
		public function onStart(ev:CommandEvent):void
		{
			//trace("The move command was started");
		}
		public function onProgress(ev:CommandEvent):void
		{
			//trace("Progress of Move Command "+ev.getProgress());
		}
		///////////////properties of all actors//////////////////////////////
		public function setWeight(value:Number):void
		{
			//
		}
		public function setHeight(value:Number):void
		{
			//
		}
		public function setTilePosition(value:*):void
		{
			//switch * to Tile once Tile class is created
		}
		public function setSpeed(value:Number):void
		{
			__speed = value;
		}
		public function setAccelleration(value:Number):void
		{
			//
		}
		//setWeapon?
		//setArmor?
		//setVehicle?
		//the above could all be decorators...t.w.
	}
}